<template>
  <div class="department-management">
    <TopNavigation :showOrganizationSection="false" :showTaskRoleSection="false" />
    <div class="page-header">
      <h2>部门管理</h2>
    </div>

    <div class="card">
      <div class="toolbar">
        <div class="search-container">
          <div class="input-group">
            <input type="text" class="input" placeholder="搜索部门名称或英文名" v-model="searchKeyword"
              @keyup.enter="searchDepartments" />
            <button class="button--submit" @click="searchDepartments">
              搜索
            </button>
          </div>
        </div>
        <button class="btn btn-primary" @click="showAddModal">新建部门</button>
      </div>

      <div class="table-container">
        <!-- 加载状态 -->
        <div v-if="loading" class="loading-state">
          <div class="spinner"></div>
          <p>加载中...</p>
        </div>

        <!-- 部门卡片展示 -->
        <div v-else class="departments-grid">
          <div v-for="department in paginatedDepartments" :key="department.deptId" class="department-card">
            <div class="department-header">
              <div class="department-title-section">
                <h3 class="department-name">{{ department.deptName }}</h3>
                <span class="department-en-name">{{ department.deptEnName || '未设置英文名' }}</span>
              </div>
              <div class="department-actions">
                <button class="edit-btn" @click="showEditModal(department)" title="编辑部门">
                  ✏️
                </button>
                <button class="delete-btn" @click="showDeleteConfirm(department)" title="删除部门">
                  🗑️
                </button>
              </div>
            </div>
            
            <div class="department-content">
              <div class="department-description-section">
                <div class="section-title">部门说明</div>
                <div class="department-description">{{ department.deptDesc || '暂无说明' }}</div>
              </div>
            </div>
          </div>
        </div>

        <!-- 空状态提示 -->
        <div v-if="!loading && paginatedDepartments.length === 0" class="empty-state">
          <div class="empty-icon">🏢</div>
          <div class="empty-text">暂无部门数据</div>
          <div class="empty-hint">点击上方按钮添加部门</div>
        </div>

        <!-- 分页 -->
        <div v-if="!loading && paginatedDepartments.length > 0" class="pagination-container">
          <div class="pagination">
            <button class="btn btn-default" :disabled="page === 1" @click="onPageChange(page - 1)">上一页</button>
            <span class="page-info">第 {{ page }} 页 / 共 {{ Math.ceil(totalItems / pageSize) }} 页</span>
            <button class="btn btn-default" :disabled="page >= Math.ceil(totalItems / pageSize)"
              @click="onPageChange(page + 1)">下一页</button>
            <div class="page-size-selector">
              <span>每页显示:</span>
              <select class="select" v-model="pageSize" @change="onPageSizeChange">
                <option :value="5">5条</option>
                <option :value="10">10条</option>
                <option :value="20">20条</option>
                <option :value="50">50条</option>
              </select>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 新增/编辑部门模态框 -->
    <div v-if="showModal" class="modal">
      <div class="modal-content">
        <div class="modal-header">
          <h3 class="modal-title">{{ isEditMode ? '编辑部门' : '新建部门' }}</h3>
          <button class="btn btn-default" @click="closeModal">×</button>
        </div>
        <div class="modal-body">
            <!-- 错误信息显示区域 -->
            <div v-if="showError" class="error-message">
              <div class="error-icon">⚠️</div>
              <div class="error-text">{{ errorMessage }}</div>
            </div>
            
            <div class="form-item">
              <label class="form-label">部门名称</label>
              <input type="text" class="input" v-model="formData.deptName" placeholder="请输入部门名称" />
            </div>
            <div class="form-item">
              <label class="form-label">部门英文名</label>
              <input type="text" class="input" v-model="formData.deptEnName" placeholder="请输入部门英文名" />
            </div>
            <div class="form-item">
              <label class="form-label">部门说明</label>
              <textarea class="input" v-model="formData.deptDesc" placeholder="请输入部门说明" rows="3"></textarea>
            </div>
          </div>
        <div class="modal-footer">
          <button class="btn btn-default" @click="closeModal">取消</button>
          <button class="btn btn-primary" @click="saveDepartment">{{ isEditMode ? '更新' : '确定' }}</button>
        </div>
      </div>
    </div>

    <!-- 删除确认模态框 -->
    <div v-if="showDeleteModal" class="modal">
      <div class="modal-content delete-modal">
        <div class="modal-header">
          <h3 class="modal-title">确认删除</h3>
          <button class="btn btn-default" @click="closeDeleteModal">×</button>
        </div>
        <div class="modal-body">
          <div class="delete-warning">
            <div class="warning-icon">🗑️</div>
            <div class="warning-text">
              <p>您确定要删除部门 <strong>"{{ deleteTargetName }}"</strong> 吗？</p>
              <p class="warning-note">此操作不可撤销，请谨慎操作。</p>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button class="btn btn-default" @click="closeDeleteModal">取消</button>
          <button class="btn btn-danger" @click="confirmDelete">确认删除</button>
        </div>
      </div>
    </div>


  </div>
</template>

<script setup>
import TopNavigation from '../components/TopNavigation.vue'
import { ref, computed, onMounted, watch } from 'vue'
import { departmentService } from '../services/departmentService.js'

// 初始化部门数据
const departments = ref([])
const loading = ref(true)
const error = ref(null)

// 从API获取部门数据
const fetchDepartments = async () => {
  try {
    loading.value = true
    console.log('正在调用API获取部门数据...')
    
    // 构建请求参数（不含page和size）
    const params = {}
    
    // 添加搜索参数
    if (searchKeyword.value.trim()) {
      params.search = searchKeyword.value.trim()
    }
    
    const response = await departmentService.getDepartments(params)
    console.log('部门数据API响应:', response)
    if (response.code === 200) {
      departments.value = response.data?.content || response.data || []
      totalItems.value = response.data?.totalElements || response.data?.total || departments.value.length
      console.log('成功获取部门数据，共', departments.value.length, '条，总计', totalItems.value, '条')
    } else {
      throw new Error(response.message || '获取部门数据失败')
    }
  } catch (err) {
    error.value = err.message || '获取部门数据时发生错误'
    console.error('Failed to fetch departments:', err)
    // 显示错误消息
    showMessage(error.value, 'error')
  } finally {
    loading.value = false
  }
}

// 组件挂载时获取部门数据
onMounted(() => {
  fetchDepartments()
})

// 搜索关键词
const searchKeyword = ref('')

// 过滤后的部门数据（基于搜索关键词）
const filteredDepartments = computed(() => {
  if (!searchKeyword.value.trim()) {
    return departments.value;
  }
  
  const keyword = searchKeyword.value.trim().toLowerCase();
  return departments.value.filter(dept => 
    dept.deptName.toLowerCase().includes(keyword) || 
    (dept.deptEnName && dept.deptEnName.toLowerCase().includes(keyword))
  );
})

// 模态框相关状态
const showModal = ref(false)
const isEditMode = ref(false)
const currentDepartmentId = ref(null)
const formData = ref({
  deptName: '',
  deptEnName: '',
  deptDesc: ''
})

// 弹窗内错误信息状态
const errorMessage = ref('')
const showError = ref(false)

// 删除确认弹窗状态
const showDeleteModal = ref(false)
const deleteTargetId = ref(null)
const deleteTargetName = ref('')

// 排序状态
const sortField = ref('')
const sortOrder = ref('asc') // 'asc' 或 'desc'

// 分页数据
const page = ref(1)
const pageSize = ref(10)
const totalItems = ref(0)

// 排序功能
const sortBy = (field) => {
  if (sortField.value === field) {
    // 如果点击的是当前排序字段，切换排序方向
    sortOrder.value = sortOrder.value === 'asc' ? 'desc' : 'asc'
  } else {
    // 如果点击的是新字段，设置为升序
    sortField.value = field
    sortOrder.value = 'asc'
  }
}

// 排序后的部门列表
const sortedDepartments = computed(() => {
  if (!sortField.value) {
    return filteredDepartments.value
  }

  return [...filteredDepartments.value].sort((a, b) => {
    const aValue = a[sortField.value] || ''
    const bValue = b[sortField.value] || ''

    if (sortOrder.value === 'asc') {
      return aValue.localeCompare(bValue, 'zh-CN')
    } else {
      return bValue.localeCompare(aValue, 'zh-CN')
    }
  })
})

// 处理分页大小变化
const onPageSizeChange = () => {
  // 重置到第一页并重新获取数据
  page.value = 1
  fetchDepartments()
}

// 处理页码变化
const onPageChange = (newPage) => {
  page.value = newPage
  fetchDepartments()
}

// 搜索部门
const searchDepartments = () => {
  // 重置到第一页并重新获取数据
  page.value = 1
  fetchDepartments()
}

// 分页后的部门数据
const paginatedDepartments = computed(() => {
  // 计算起始和结束索引
  const startIndex = (page.value - 1) * pageSize.value
  const endIndex = startIndex + pageSize.value
  // 返回当前页的数据（使用排序和过滤后的数据）
  return sortedDepartments.value.slice(startIndex, endIndex)
})



// 显示新增模态框
const showAddModal = () => {
  isEditMode.value = false
  currentDepartmentId.value = null
  formData.value = {
    deptName: '',
    deptEnName: '',
    deptDesc: ''
  }
  showModal.value = true
}

// 显示编辑模态框
const showEditModal = (department) => {
  isEditMode.value = true
  currentDepartmentId.value = department.deptId
  formData.value = {
    deptName: department.deptName,
    deptEnName: department.deptEnName || '',
    deptDesc: department.deptDesc
  }
  showModal.value = true
}

// 关闭模态框
const closeModal = () => {
  showModal.value = false
  errorMessage.value = ''
  showError.value = false
}

// 保存部门
const saveDepartment = async () => {
  // 清除之前的错误信息
  errorMessage.value = ''
  showError.value = false

  // 表单验证
  if (!formData.value.deptName.trim()) {
    errorMessage.value = '请输入部门名称'
    showError.value = true
    return
  }
  if (!formData.value.deptEnName.trim()) {
    errorMessage.value = '请输入部门英文名'
    showError.value = true
    return
  }

  // 检查部门名称是否重复（不区分大小写）
    if (!isEditMode.value) {
      // 新增模式
      // 检查部门名称
      const nameDuplicate = departments.value.some(dept =>
        dept.deptName.toLowerCase() === formData.value.deptName.toLowerCase()
      );
      if (nameDuplicate) {
        errorMessage.value = '部门已存在'
        showError.value = true
        return
      }

      // 检查部门英文名
      const enNameDuplicate = departments.value.some(dept =>
        dept.deptEnName && dept.deptEnName.toLowerCase() === formData.value.deptEnName.toLowerCase()
      );
      if (enNameDuplicate) {
        errorMessage.value = '部门已存在'
        showError.value = true
        return
      }
    } else {
      // 编辑模式下，检查除当前部门外是否有重名
      // 检查部门名称
      const nameDuplicate = departments.value.some(dept =>
        dept.deptId !== currentDepartmentId.value &&
        dept.deptName.toLowerCase() === formData.value.deptName.toLowerCase()
      );
      if (nameDuplicate) {
        errorMessage.value = '部门已存在'
        showError.value = true
        return
      }

      // 检查部门英文名
      const enNameDuplicate = departments.value.some(dept =>
        dept.deptId !== currentDepartmentId.value &&
        dept.deptEnName && dept.deptEnName.toLowerCase() === formData.value.deptEnName.toLowerCase()
      );
      if (enNameDuplicate) {
        errorMessage.value = '部门已存在'
        showError.value = true
        return
      }
    }

  try {
    // 显示加载状态
    loading.value = true;

    if (isEditMode.value) {
      // 更新部门
      console.log('正在调用API更新部门:', currentDepartmentId.value, formData.value)
      const response = await departmentService.updateDepartment(currentDepartmentId.value, {
        deptName: formData.value.deptName,
        deptEnName: formData.value.deptEnName,
        deptDesc: formData.value.deptDesc
      })
      console.log('更新部门API响应:', response)
      if (response.code === 200) {
        // 更新本地数据
        const index = departments.value.findIndex(dept => dept.deptId === currentDepartmentId.value)
        if (index !== -1) {
          departments.value[index] = {
            ...departments.value[index],
            deptName: formData.value.deptName,
            deptEnName: formData.value.deptEnName,
            deptDesc: formData.value.deptDesc
          }
        }
        showMessage('部门更新成功', 'success')
        // 关闭弹窗
        closeModal()
      } else {
        throw new Error(response.message || '更新部门失败，状态码：' + response.code)
      }
    } else {
      // 新增部门
      console.log('正在调用API创建部门:', formData.value)
      const response = await departmentService.createDepartment({
        deptName: formData.value.deptName,
        deptEnName: formData.value.deptEnName,
        deptDesc: formData.value.deptDesc
      })
      console.log('创建部门API响应:', response)
      if (response.code === 201) {
        // 如果有返回数据，添加到列表中
        if (response.data) {
          departments.value.push(response.data)
          console.log('成功添加新部门:', response.data)
        } else {
          // 如果没有返回数据，重新获取部门列表
          await fetchDepartments()
        }
        showMessage('部门创建成功', 'success')
        // 关闭弹窗
        closeModal()
      } else {
        throw new Error(response.message || '创建部门失败，状态码：' + response.code)
      }
    }
  } catch (err) {
    showMessage(err.message || '操作失败', 'error')
    console.error('Failed to save department:', err);
    console.error('错误详情:', err.response || err);
  } finally {
    // 隐藏加载状态
    loading.value = false;
  }
}

// 显示删除确认弹窗
const showDeleteConfirm = (department) => {
  deleteTargetId.value = department.deptId
  deleteTargetName.value = department.deptName
  showDeleteModal.value = true
}

// 关闭删除确认弹窗
const closeDeleteModal = () => {
  showDeleteModal.value = false
  deleteTargetId.value = null
  deleteTargetName.value = ''
}

// 确认删除部门
const confirmDelete = async () => {
  try {
    console.log('正在调用API删除部门:', deleteTargetId.value)
    const response = await departmentService.deleteDepartment(deleteTargetId.value)
    console.log('删除部门API响应:', response)
    if (response.code === 200) {
      // 更新本地数据
      departments.value = departments.value.filter(dept => dept.deptId !== deleteTargetId.value)
      showMessage('部门删除成功', 'success')
      closeDeleteModal()
    } else {
      throw new Error(response.message || '删除部门失败')
    }
  } catch (err) {
    showMessage(err.message || '删除失败', 'error')
    console.error('Failed to delete department:', err)
  }
}

// 消息提示函数 - 确保显示在界面中间
const showMessage = (message, type = 'info') => {
  // 创建消息元素
  const msgElement = document.createElement('div')
  msgElement.className = `message message-${type} show`
  msgElement.textContent = message
  
  // 添加内联样式确保在中间显示
  msgElement.style.position = 'fixed'
  msgElement.style.top = '50%'
  msgElement.style.left = '50%'
  msgElement.style.transform = 'translate(-50%, -50%)'
  msgElement.style.padding = '16px 24px'
  msgElement.style.borderRadius = '8px'
  msgElement.style.color = 'white'
  msgElement.style.fontSize = '14px'
  msgElement.style.fontWeight = '500'
  msgElement.style.zIndex = '9999'
  msgElement.style.boxShadow = '0 4px 16px rgba(0, 0, 0, 0.2)'
  msgElement.style.textAlign = 'center'
  msgElement.style.minWidth = '200px'
  msgElement.style.maxWidth = '400px'
  
  // 根据消息类型设置背景色
  if (type === 'error') {
    msgElement.style.backgroundColor = '#ff4d4f'
    msgElement.style.borderLeft = '4px solid #cf1322'
  } else if (type === 'success') {
    msgElement.style.backgroundColor = '#52c41a'
    msgElement.style.borderLeft = '4px solid #389e0d'
  } else {
    msgElement.style.backgroundColor = '#1890ff'
    msgElement.style.borderLeft = '4px solid #096dd9'
  }

  // 添加到body
  document.body.appendChild(msgElement)

  // 5秒后移除（延长显示时间）
  setTimeout(() => {
    msgElement.style.opacity = '0'
    msgElement.style.transition = 'opacity 0.3s ease'
    setTimeout(() => {
      if (document.body.contains(msgElement)) {
        document.body.removeChild(msgElement)
      }
    }, 300)
  }, 5000)
}
</script>

<style scoped>
.department-management {
  width: 100%;
  padding: 20px;
}

.page-header {
  margin-bottom: 20px;
}

.page-header h2 {
  font-size: 24px;
  margin: 0;
}

.card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  border: 1px solid #e0e0e0;
  overflow: hidden;
}

/* 部门卡片容器 - 响应式网格布局 */
.departments-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(380px, 1fr));
  gap: 24px;
  margin-bottom: 20px;
}

/* 部门卡片样式 */
.department-card {
  background: #fff;
  border: 1px solid #e0e0e0;
  border-radius: 12px;
  padding: 24px;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  min-height: 160px;
}

.department-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
  border-color: #007bff;
}

/* 部门头部 */
.department-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid #f0f0f0;
}

.department-title-section {
  flex: 1;
}

.department-name {
  margin: 0 0 6px 0;
  font-size: 22px;
  font-weight: 600;
  color: #333;
  line-height: 1.4;
}

.department-en-name {
  font-size: 13px;
  color: #666;
  background: #f8f9fa;
  padding: 4px 8px;
  border-radius: 4px;
  font-style: italic;
}

.department-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* 部门内容 */
.department-content {
  margin-bottom: 16px;
}

.department-description-section {
  margin-bottom: 16px;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #ffffff 0%, #f8f9fa 100%);
  border: 2px solid #e5e7eb;
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  transition: all 0.3s ease;
}

.department-description-section:hover {
  border-color: #667eea;
  box-shadow: 0 4px 16px rgba(102, 126, 234, 0.15);
  transform: translateY(-2px);
  background: linear-gradient(135deg, #f0f4ff 0%, #ffffff 100%);
}

.section-title {
  font-size: 12px;
  color: #495057;
  margin-bottom: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  padding: 6px 0;
  border-bottom: 2px solid #e9ecef;
  position: relative;
  transition: all 0.3s ease;
}

.section-title::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 30px;
  height: 2px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  transition: width 0.3s ease;
}

.section-title:hover::after {
  width: 60px;
}

.department-description {
  font-size: 14px;
  color: #555;
  line-height: 1.5;
  width: 100%;
  background: linear-gradient(135deg, #f8f9fa 0%, #ffffff 100%);
  border: 1px solid #e9ecef;
  border-radius: 8px;
  padding: 12px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.04);
  transition: all 0.3s ease;
  margin-bottom: 8px;
}

.department-description:hover {
  border-color: #007bff;
  box-shadow: 0 2px 8px rgba(0, 123, 255, 0.15);
  background: linear-gradient(135deg, #e3f2fd 0%, #ffffff 100%);
}

/* 部门操作按钮 */
.edit-btn,
.delete-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px;
  border-radius: 6px;
  transition: background-color 0.2s ease;
  font-size: 18px;
}

.edit-btn:hover {
  background-color: #e3f2fd;
}

.delete-btn:hover {
  background-color: #ffebee;
}

.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  background: #f8f9fa;
  border-bottom: 1px solid #e0e0e0;
  flex-wrap: wrap;
  gap: var(--spacing-md);
}

.search-container {
  display: flex;
  gap: 12px;
  align-items: center;
  flex: 1;
  min-width: 280px;
}

.input-group {
  display: flex;
  gap: 12px;
  align-items: center;
}

.input {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
}

.input:focus {
  outline: none;
  border-color: #007bff;
}

/* 按钮样式已移至 theme.css 统一管理 */

.table-container {
  overflow-x: auto;
}

.table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.table th {
  padding: 12px;
  text-align: left;
  background: #f8f9fa;
  border-bottom: 1px solid #dee2e6;
}

.table td {
  padding: 12px;
  text-align: left;
  border-bottom: 1px solid #dee2e6;
}

.table tbody tr:hover td {
  background: #f5f5f5;
}

.empty-state {
  text-align: center;
  padding: 80px 20px;
  color: var(--text-secondary);
  min-height: 300px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--spacing-md);
  background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
  border-radius: 16px;
  border: 2px dashed #cbd5e1;
  margin: 20px 0;
  transition: all 0.3s ease;
}

.empty-state:hover {
  background: linear-gradient(135deg, #f1f5f9 0%, #e2e8f0 100%);
  border-color: #94a3b8;
}

.empty-icon {
  font-size: 64px;
  opacity: 0.6;
  margin-bottom: var(--spacing-sm);
  transition: all 0.3s ease;
}

.empty-state:hover .empty-icon {
  transform: scale(1.1);
  opacity: 0.8;
}

.empty-text {
  font-size: 18px;
  font-weight: 600;
  color: #475569;
  margin-bottom: 8px;
}

.empty-hint {
  font-size: 14px;
  color: #64748b;
  font-style: italic;
  padding: 8px 16px;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 8px;
  border: 1px solid #e2e8f0;
}

.loading-state {
  text-align: center;
  padding: 40px;
  color: #666;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid #f3f3f3;
  border-top: 3px solid #007bff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}

.form-item {
  margin-bottom: 16px;
}

.form-label {
  display: block;
  margin-bottom: 6px;
  font-weight: 500;
  color: #333;
  font-size: 14px;
}

.form-item textarea.input {
  resize: vertical;
  min-height: 80px;
}

/* 现代化模态框样式 */



/* 错误信息样式 */
  .error-message {
    display: flex;
    align-items: center;
    gap: var(--spacing-xs, 8px);
    padding: var(--spacing-xs, 8px) var(--spacing-sm, 12px);
    margin-bottom: var(--spacing-sm, 12px);
    background-color: #fef3f2;
    border: 1px solid #fecaca;
    border-radius: var(--border-radius-sm, 4px);
    color: #dc2626;
    font-size: var(--font-size-xs, 12px);
    line-height: 1.4;
    width: 100%;
  }

  .error-icon {
    font-size: 14px;
    flex-shrink: 0;
  }

  .error-text {
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* 删除确认模态框内容样式 */
  .delete-warning {
    display: flex;
    align-items: flex-start;
    gap: var(--spacing-md, 16px);
    padding: var(--spacing-lg, 24px);
    background-color: #fef3f2;
    border: 1px solid #fecaca;
    border-radius: var(--border-radius-md, 8px);
  }

  .warning-icon {
    font-size: 24px;
    flex-shrink: 0;
  }

  .warning-text {
    flex: 1;
  }

  .warning-text p {
    margin: 0 0 var(--spacing-sm, 8px) 0;
    color: var(--text-primary, #333);
    line-height: 1.5;
  }

  .warning-text p:last-child {
    margin-bottom: 0;
  }

  .warning-note {
    font-size: var(--font-size-sm, 14px);
    color: var(--text-secondary, #666);
    font-style: italic;
  }

  .warning-text strong {
    color: #dc2626;
    font-weight: 600;
  }

  /* 部门已存在提示弹窗样式 */
  .warning-modal {
    width: 360px;
  }

  .warning-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 24px 0;
    text-align: center;
  }

  .warning-content .warning-icon {
    margin-bottom: 16px;
  }

  .warning-message {
    font-size: 16px;
    color: #333;
    font-weight: 500;
  }

/* 现代化分页样式 */
.pagination-container {
  padding: 24px;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.9) 0%, rgba(247, 250, 252, 0.9) 100%);
  border-top: var(--border-modern);
  backdrop-filter: blur(5px);
}

.pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  flex-wrap: wrap;
}

.page-info {
  color: #667eea;
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  padding: 8px 16px;
  background: rgba(102, 126, 234, 0.1);
  border-radius: 8px;
}

.page-size-selector {
  display: flex;
  align-items: center;
  gap: 12px;
  color: #667eea;
  font-size: 14px;
  font-weight: 600;
}

.select {
  padding: 8px 12px;
  border: 2px solid rgba(102, 126, 234, 0.2);
  border-radius: 8px;
  font-size: 14px;
  background: rgba(255, 255, 255, 0.9);
  color: #667eea;
  font-weight: 600;
  transition: all 0.3s ease;
}

.select:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

/* 消息提示样式 */
.message {
  position: fixed;
  top: 20px;
  right: 20px;
  padding: 12px 20px;
  border-radius: 6px;
  color: white;
  font-weight: 500;
  font-size: 14px;
  z-index: 9999;
  min-width: 200px;
  max-width: 400px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  transition: all 0.3s ease;
  animation: slideInRight 0.3s ease;
}

.message-success {
  background-color: #52c41a;
  border-left: 4px solid #389e0d;
}

.message-error {
  background-color: #ff4d4f;
  border-left: 4px solid #cf1322;
}

.message-info {
  background-color: #1890ff;
  border-left: 4px solid #096dd9;
}

.message-warning {
  background-color: #faad14;
  border-left: 4px solid #d48806;
}

@keyframes slideInRight {
  from {
    opacity: 0;
    transform: translateX(100%);
  }

  to {
    opacity: 1;
    transform: translateX(0);
  }
}

/* 现代化排序样式 */
.sortable {
  cursor: pointer;
  user-select: none;
  position: relative;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.sortable:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.sort-icon {
  margin-left: 8px;
  font-size: 14px;
  color: rgba(255, 255, 255, 0.9);
  font-weight: bold;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
  transition: all 0.3s ease;
}

.sortable:hover .sort-icon {
  color: white;
  transform: scale(1.2);
}

/* 删除确认模态框样式 */
.delete-modal {
  max-width: 450px;
}

.delete-warning {
  display: flex;
  align-items: flex-start;
  gap: var(--spacing-md, 16px);
  padding: var(--spacing-lg, 24px);
  background-color: #fef3f2;
  border: 1px solid #fecaca;
  border-radius: var(--border-radius-md, 8px);
}

.warning-icon {
  font-size: 24px;
  flex-shrink: 0;
}

.warning-text {
  flex: 1;
}

.warning-text p {
  margin: 0 0 var(--spacing-sm, 8px) 0;
  color: var(--text-primary, #333);
  line-height: 1.5;
}

.warning-text p:last-child {
  margin-bottom: 0;
}

.warning-note {
  font-size: var(--font-size-sm, 14px);
  color: var(--text-secondary, #666);
  font-style: italic;
}

.warning-text strong {
  color: #dc2626;
  font-weight: 600;
}

.delete-modal .modal-footer {
  justify-content: center;
  gap: var(--spacing-lg, 24px);
}

.delete-modal .btn-danger {
  min-width: 100px;
}

.delete-modal .btn-default {
  min-width: 80px;
}

/* 现代化响应式设计 */
@media (max-width: 768px) {
  .department-management {
    padding: 12px;
  }

  .page-header h2 {
    font-size: 24px;
  }

  .toolbar {
    flex-direction: column;
    align-items: stretch;
    padding: 20px;
    gap: 16px;
  }

  .search-container {
    width: 100%;
  }

  .btn {
    padding: 10px 20px;
    font-size: 13px;
  }

  .table th {
    padding: 16px 12px;
    font-size: 12px;
  }

  .table td {
    padding: 14px 12px;
    font-size: 13px;
  }

  .modal-content {
    width: 95%;
    margin: 10px;
    border-radius: 16px;
  }

  .modal-header,
  .modal-body,
  .modal-footer {
    padding: 20px;
  }

  .message {
    top: 10px;
    right: 10px;
    left: 10px;
    max-width: none;
    border-radius: 12px;
  }

  .pagination {
    flex-direction: column;
    gap: 12px;
  }

  .page-size-selector {
    justify-content: center;
  }
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .departments-grid {
    grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
    gap: 20px;
  }
}

@media (max-width: 768px) {
  .departments-grid {
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 16px;
  }
  
  .department-card {
    padding: 20px;
  }
  
  .department-name {
    font-size: 18px;
  }
  
  .department-en-name {
    font-size: 12px;
  }
}

@media (max-width: 480px) {
  .department-management {
    padding: 8px;
  }

  .page-header h2 {
    font-size: 20px;
  }

  .card {
    border-radius: 12px;
  }
  
  .departments-grid {
    grid-template-columns: 1fr;
    gap: 8px;
  }
  
  .department-card {
    padding: 12px;
  }
  
  .department-name {
    font-size: 16px;
  }
  
  .department-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
  
  .department-actions {
    align-self: flex-end;
  }

  .btn {
    padding: 8px 16px;
    font-size: 12px;
  }

  .modal-content {
    width: 98%;
    margin: 5px;
  }
}
</style>